﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DTO;
using DAO;


namespace BUS
{
    public class KhachHang_BUS
    {
        private static KhachHang_BUS m_instance = null;

        public static KhachHang_BUS Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new KhachHang_BUS();
                return m_instance;
            }
        }

        /// <summary>
        /// Lấy thông tin khách hàng theo mã khách hàng
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public KhachHang_DTO LayThongTinKhachHang(string id)
        {
            DataTable dt = KhachHang_DAO.Instance.LayKhachHang(id);
            // Bi loi
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return null;

            DataRow row = list[0];
            string makh = (string)row["MaKhachHang"];
            string tenkh = (string)row["TenKhachHang"];
            string diachi = (string)row["DiaChi"];
            int thanthiet = (int)row["LoaiKhachHang"];

            return new KhachHang_DTO(makh, tenkh, diachi, thanthiet == 1 ? true : false);
        }

        /// <summary>
        /// Lấy DS Khách Hàng
        /// </summary>
        /// <returns></returns>
        public List<KhachHang_DTO> LayToanBoKhachHang()
        {
            List<KhachHang_DTO> result = new List<KhachHang_DTO>();

            DataTable dt = KhachHang_DAO.Instance.LayToanBoKhachHang();
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return result;
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = list[i];
                string makh = (string)row["MaKhachHang"];
                string tenkh = (string)row["TenKhachHang"];
                string diachi = (string)row["DiaChi"];
                int thanthiet = (int)row["LoaiKhachHang"];
                result.Add(new KhachHang_DTO(makh, tenkh, diachi, thanthiet == 1 ? true : false));
            }
            return result;
        }

        /// <summary>
        /// Lấy toàn bộ mã khách hàng
        /// </summary>
        /// <returns></returns>
        public List<string> LayToanBoMaKhachHang()
        {
            List<string> result = new List<string>();

            DataTable dt = KhachHang_DAO.Instance.LayToanBoMaKhachHang();
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return result;
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = list[i];
                string makh = (string)row["MaKhachHang"];
                result.Add(makh);
            }
            return result;
        }

        /// <summary>
        /// Lấy toàn bộ mã khách hàng thân quen
        /// </summary>
        /// <returns></returns>
        public List<string> LayToanBoMaKhachHangQuen()
        {
            List<string> result = new List<string>();

            DataTable dt = KhachHang_DAO.Instance.LayToanBoMaKhachHangQuen();
            if (dt == null)
                return result;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return result;
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = list[i];
                string makh = (string)row["MaKhachHang"];
                result.Add(makh);
            }
            return result;
        }

        /// <summary>
        ///   tìm kiếm khách hàng
        /// </summary>
        /// 
        public List<KhachHang_DTO> TimKiemKhachHang(KhachHang_DTO _khachHang)
        {
            List<KhachHang_DTO> result = new List<KhachHang_DTO>();
            DataTable dt = KhachHang_DAO.Instance.TimKiemKhachHang(_khachHang.MaKhachHang, _khachHang.TenKhachHang, _khachHang.DiaChi, _khachHang.ThanThiet);
            if (dt == null)
            {
                return null;
            }
            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return result;
            }
            for (int i = 0; i < list.Count; i++)
            {
                DataRow dr = list[i];
                string makh = (string)dr["MaKhachHang"];
                string tenkh = (string)dr["TenKhachHang"];
                string diachi = (string)dr["DiaChi"];
                int thanthiet = (int)dr["LoaiKhachHang"];
                result.Add(new KhachHang_DTO(makh, tenkh, diachi, thanthiet == 1 ? true : false));
            }
            return result;
        }

        /// <summary>
        /// Cập nhật thông tin khách hàng theo mã khách hàng
        /// </summary>
        /// <param name="kh"></param>
        /// <returns></returns>
        public bool CapNhatKhachHang(KhachHang_DTO kh)
        {
            return KhachHang_DAO.Instance.CapNhatKhachHang(kh.MaKhachHang, kh.TenKhachHang, kh.DiaChi, kh.ThanThiet);
        }

        /// <summary>
        /// Xóa khách hàng
        /// </summary>
        /// <param name="kh"></param>
        /// <returns></returns>
        public bool XoaKhachHang(KhachHang_DTO kh)
        {
            return KhachHang_DAO.Instance.XoaKhachHang(kh.MaKhachHang);
        }

        public bool ThemKhachHang(KhachHang_DTO kh)
        {
            return KhachHang_DAO.Instance.ThemKhachHangMoi(kh.TenKhachHang, kh.DiaChi, kh.ThanThiet);
        }
    }
}
